<template>
  <div class="wrap">
    <div class="page-head">
      <a-breadcrumb>
        <a-breadcrumb-item>{{$t('spectIdentity.spectIdentity.62f23m4b4dw0')}}</a-breadcrumb-item>
      </a-breadcrumb>
    </div>
    <c-table-search :loading="form.loading" v-model:model="form.data" @refresh="getData">
      <a-col :span="8">
        <a-form-item field="name" :label="$t('spectIdentity.spectIdentity.62f23m4b4dw1')">
          <a-input v-model="form.data.name" :placeholder="$t('spectIdentity.spectIdentity.62f23m4b4dw2')" />
        </a-form-item>
      </a-col>
      <template v-slot:refresh>
        <a-button type="outline" @click="refresh">
          <template #icon>
            <icon-loop />
          </template>
        </a-button>
      </template>
      <template v-slot:add>
        <a-button type="primary" v-permission="['spectIdentity:add']" @click="form.create.show = true">
          <template #icon>
            <icon-plus />
          </template>
          {{$t('spectIdentity.spectIdentity.62f23m4bfsc0')}}
        </a-button>
      </template>
      <template v-slot:inData>
        <a-button type="primary" v-permission="['spectIdentity:inData']" @click="toData">
          <template #icon>
            <icon-import />
          </template>
          {{$t('spectIdentity.spectIdentity.62f23m4bg200')}}
        </a-button>
      </template>
      <template v-slot:downloadTem>
        <a-button type="primary" v-permission="['spectIdentity:downloadTem']">
          <template #icon>
            <icon-download />
          </template>
          {{$t('spectIdentity.spectIdentity.62f23m4bg201')}}
        </a-button>
      </template>
    </c-table-search>
    <div class="table-scroll">
      <c-table :list="form.list" :loading="form.loading" :count="form.count" v-model:data="form.data"
        @refresh="getData">
        <a-table-column :title="$t('spectIdentity.spectIdentity.60zeizn0jbg0')" ellipsis tooltip :width="140">
          <template #cell="{ record }">
            {{ record.id ? record.id : '-' }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('spectIdentity.spectIdentity.60zeizn0jbg2')" ellipsis tooltip :width="150">
          <template #cell="{ record }">
            {{ record.type ? record.type : '-' }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('spectIdentity.spectIdentity.60zeizn0jbg3')" ellipsis tooltip :width="150">
          <template #cell="{ record }">
            {{ record.type ? record.type : '-' }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('spectIdentity.spectIdentity.60zeizn0jbg4')" ellipsis tooltip :width="200">
          <template #cell="{ record }">
            {{ record.type ? record.type : '-' }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('spectIdentity.spectIdentity.60zeizn0jbg5')" ellipsis tooltip :width="320">
          <template #cell="{ record }">
            {{ record.province }}{{ record.district }}{{ record.address }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('spectIdentity.spectIdentity.60zeizn0jbg6')" ellipsis tooltip :width="320">
          <template #cell="{ record }">
            {{ record.province }}{{ record.district }}{{ record.address }}
          </template>
        </a-table-column>
        <a-table-column :title="$t('device.device.60zedd3f14c0')" fixed='right' :width="200">
          <template #cell="{ record }">
            <a-space>
              <a-button type="text" v-permission="['spectIdentity:edit']"
                @click="handleUpdata(record)">{{$t('spectIdentity.spectIdentity.62f23m4bg6o0')}}</a-button>
              <a-button type="text" v-permission="['spectIdentity:del']" status="danger"
                @click="handleDelete(record)">{{$t('spectIdentity.spectIdentity.62f23m4bgb00')}}</a-button>
            </a-space>
          </template>
        </a-table-column>
      </c-table>
      <Create @refresh="getData" v-model:show="form.create.show"></Create>
      <Updata @refresh="getData" v-model:show="form.update.show" v-model:data="form.update.data"></Updata>
    </div>
  </div>
</template>

<script lang='ts' setup>
import { apiDeviceList, apiDeviceDelete } from '@/api/device'
const Create = defineAsyncComponent(() => import('./components/create.vue'))
const Updata = defineAsyncComponent(() => import('./components/updata.vue'))
const temp = useTemp()
const local = useLocal()
const emit = defineEmits(['refresh'])
const refresh = () => {
  emit('refresh')
}
const form = reactive({
  create: {
    show: false
  },
  update: {
    loading: false,
    show: false,
    data: {},
  },
  delete: {
    show: false
  },
  // auths:[],
  // allAuths:[],
  data: {
    name: '',
    uid: local.usermsg.id == 3 ? 0 : local.usermsg.id
  },
  loading: false,
  list: [],
  count: 0
})
// 无人机绑定信息列表
const getData = async () => {
  form.loading = true
  const { code, data } = await apiDeviceList({
    ...form.data
  })
  form.loading = false
  if (code != 200) return;
  form.list = data.list
  form.count = data.total

}
// 编辑
const handleUpdata = (record: any) => {
  form.update.data = cloneDeep(record)
  form.update.show = true
}
// 删除
const handleDelete = async (record: any) => {
  useModal.warning({
    content: t('device.device.60zedd3f1sg0'),
    onBeforeOk: async () => {
      const { code } = await apiDeviceDelete({
        id: record.id
      })
      if (code != 200) return false;
      useMessage.success()
      getData()
      return true
    }
  })
}
//设备组管理
const toData =  () => {
  console.log(t('spectIdentity.spectIdentity.62f23m4bg200'))
}
// const downloadTem = () => {
//   console.log(t('spectIdentity.spectIdentity.62f23m4bg201'))
// }
onBeforeMount(() => {
  getData()
  temp.getDeviceGroupList()
})
</script>

<style scoped lang="less">
.show {
  display: none
}

.color {
  width: 30px;
  height: 30px;
  border-radius: 2px;
}
</style>
